Vue.component('manage-company', {
  template: `
    <el-autocomplete
            v-model="form.manage_company_name"
            class="inline-input"
            :fetch-suggestions="companyQuerySearch"
            placeholder="管理公司"
            :trigger-on-focus="false"
            :maxlength="100"
            @select="companyHandleSelect"
        >
        </el-autocomplete>
  `,
  props: {
    value: {
      type: String,
      default: ''
    },
    form: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      tycParam: {},
      dataId: undefined,
      queryParams: {},
      trigger: true
    }
  },
  created() {
    this.dataId = window.parent.dataId
  },
  mounted() {
  },
  watch: {
    'form': {
      handler(val) {
        this.$nextTick(() => {
          if (this.form.id && this.trigger) {
            this.trigger = false
            axios({
              url: window.parent.baseUrl + '/base/gpBase/' + val.manage_company,
              headers: {
                'Content-Type':'application/json;charset=utf-8',
                'Authorization': window.parent.token
              },
              method: 'get'
            }).then(res => {
              let _res = res.data
              if (_res.code === 200) {
                this.form.manage_company_name = _res.data.name
              }
            })
          }
        })
      },
      deep: true
    }
  },
  methods: {
    companyQuerySearch(queryString, cb) {
      axios({
        url: window.parent.baseUrl + '/base/gpBase/gpListByName?gpName=' + queryString,
        headers: {
          'Content-Type':'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get'
      }).then(res => {
        let _res = res.data
        if (_res.code === 200) {
          cb(_res.data)
          const item = { name: this.form.manage_company_name, shortName: this.form.manage_company_alias }
          this.companyHandleSelect(item)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      // GP名称
      this.form.manage_company_name = item.name
      // GP简称
      this.form.manage_company_alias = item.shortName
    }
  }
})
